package cn.tedu.service.impl;

import cn.tedu.config.PageData;
import cn.tedu.dao.mapper.ExperimentMapper;
import cn.tedu.pojo.dto.ExperimentDTO;
import cn.tedu.pojo.param.ExperimentAddParam;
import cn.tedu.pojo.vo.ExperimentVO;
import cn.tedu.service.ExperimentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ExperimentServiceImpl implements ExperimentService {

    @Autowired
    private ExperimentMapper experimentMapper;

    @Override
    public PageData select(ExperimentDTO equipmentQuery) {
        log.debug("查询仪器-入参：{}",equipmentQuery);
        List<ExperimentVO> list = experimentMapper.select(equipmentQuery);
        log.debug("查询仪器-:入参:{},出参:{}",equipmentQuery,list);
        //准备PageData对象用来装返回给前端的数据
        PageData dictData = new PageData();
        //向PageData对象中装存入当前页字典数据
        dictData.setList(list);
        //向PageData对象中装存入总记录数
        dictData.setTotal(experimentMapper.selectDictCount(equipmentQuery));
        //返回封装好的对象
        return dictData;
    }

    @Override
    public void createExperiment(ExperimentAddParam equipmentAddParam) {
        log.debug("创建仪器-入参：{}",equipmentAddParam);
        ExperimentDTO experimentDTO = new ExperimentDTO();
        BeanUtils.copyProperties(equipmentAddParam,experimentDTO);
        if(experimentDTO.getSerialNumber()!=null){
            experimentMapper.update(experimentDTO);
        }else {
            experimentDTO.setAddedTime(new Date());
            experimentMapper.insert(experimentDTO);
        }
    }

    @Override
    public void updateStatus(Integer serialNumber, String isCertified) {
        log.debug("修改状态业务:{}",serialNumber,isCertified);
        ExperimentDTO experimentDTO = new ExperimentDTO();
        experimentDTO.setSerialNumber(serialNumber);
        experimentDTO.setIsCertified(isCertified);
        experimentMapper.update(experimentDTO);
    }

    @Override
    public void deleteEquipment(Integer serialNumber) {
        log.debug("删除用户业务参数:{}",serialNumber);
        experimentMapper.delete(serialNumber);
    }

}
